﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Competition.Application.DTO;
using Competition.Application.Interfaces;
using Competition.Domain;
using Competition.Domain.Entity;
using Microsoft.EntityFrameworkCore;
using MiDu.Common;

namespace Competition.Application
{
    public class BannerService:BaseService<Banner>, IBannerService
    {
        public BannerService(AppDbContext dbContext) : base(dbContext)
        {
        }
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(BannerDto dto)
        {
            Banner model = new Banner()
            {
                Link = dto.Link,
                Status = dto.Status,
                Title = dto.Title,
                Url = dto.Url,
                AddTime = DateTime.Now
            };
            model = await this.GetService<Banner>().AddAsync(model);
            return model.Id;
        }
        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<BannerDto> FindAsync(int id)
        {
            var model = await this.GetService<Banner>().FindAsync(b => b.Id == id);
            BannerDto dto = new BannerDto()
            {
                Id = model.Id,
                Title = model.Title,
                Link = model.Link,
                Url = model.Url,
                Status = model.Status
            };
            return dto;
        }
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(BannerDto dto)
        {
            Banner model = await this.GetService<Banner>().FindAsync(b => b.Id == dto.Id);
            model.Link = dto.Link;
            model.Status = dto.Status;
            model.Title = dto.Title;
            model.Url = dto.Url;
            return await this.GetService<Banner>().UpdateAsync(model);
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int id)
        {
            var model = await this.GetService<Banner>().FindAsync(b => b.Id == id);
            return await this.GetService<Banner>().DeleteAsync(model);
        }
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status">状态</param>
        /// <returns></returns>
        public async Task<bool> UpdateStatusAsync(int id, int status)
        {
            var model = await this.GetService<Banner>().FindAsync(news => news.Id == id);
            model.Status = status;
            return await this.GetService<Banner>().UpdateAsync(model);
        }
        /// <summary>
        /// 分页查询数据列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>

        /// <returns></returns>
        public async Task<(PageDto pageDto, List<BannerDto> dtos)> FindListAsync(int pageIndex, int pageSize)
        {
            Expression<Func<Banner, bool>> whereLamdba = b => true;

            var v = this.GetService<Banner>().FindPageList2(pageIndex, pageSize, whereLamdba, nameof(Banner.AddTime));
            List<BannerDto> dtos = await v.list.Select(model => new BannerDto()
            {
                Id = model.Id,
                Title = model.Title,
                AddTime = model.AddTime,
                Status = model.Status,
                Link = model.Link,
                Url = model.Url
            }).ToListAsync();

            var pageDto = new PageDto()
            {
                PageSize = pageSize,
                PageIndex = pageIndex,
                NextPage = v.nextPage,
                TotalRecord = v.totalRecord,
                PrevPage = v.prevPage,
                PageCount = v.pageCount
            };
            return (pageDto, dtos);
        }
    }
}